package org.generic.component;

public class IdManager {

	// Id prefixes used to identify between different design components.
	public static final byte TEMPLATE_PREFIX 	= 0x00; // template prefix
	public static final byte THEME_PREFIX 		= 0x01; // theme prefix...
	public static final byte SCREEN_PREFIX 		= 0x02;
	public static final byte MENU_PREFIX		= 0x03;
	public static final byte ENTRY_PREFIX 		= 0x04;
	public static final byte BANNER_PREFIX 		= 0x05;
	public static final byte BUTTON_PREFIX 		= 0x06;
	public static final byte IMAGE_PREFIX 		= 0x07;
	public static final byte TEXT_PREFIX 		= 0x08;
	
	private static final ComponentType [] componentTypes = {
		new ComponentType (TEMPLATE_PREFIX, "Template"),
		new ComponentType (THEME_PREFIX, "Theme"),
		new ComponentType (SCREEN_PREFIX, "Screen"),
		new ComponentType (MENU_PREFIX, "Menu"),
		new ComponentType (ENTRY_PREFIX, "Entry"),
		new ComponentType (BANNER_PREFIX, "Banner"),
		new ComponentType (BUTTON_PREFIX, "Button"),
		new ComponentType (IMAGE_PREFIX, "Image"),
		new ComponentType (TEXT_PREFIX, "Text")
	};
	
	private static final ComponentExtension [] componentExtension = {
		new ComponentExtension (TEMPLATE_PREFIX, ".tmp"),
		new ComponentExtension (THEME_PREFIX, ".thm"),
		new ComponentExtension (SCREEN_PREFIX, ".scr"),
		new ComponentExtension (MENU_PREFIX, ".mnu"),
		new ComponentExtension (ENTRY_PREFIX, ".ent"),
		new ComponentExtension (BANNER_PREFIX, ".png"),
		new ComponentExtension (BUTTON_PREFIX, ".btn"),
		new ComponentExtension (IMAGE_PREFIX, ".png"),
		new ComponentExtension (TEXT_PREFIX, ".txt")
	};
	
	/**
	 * Receives an id and returns the prefix.
	 * @param id
	 * @return byte Prefix. 
	 * */
	public static byte getPrefixById (int id){
		int aux = id >> 16;
		return (byte) aux; 
	}
	
	public static String getComponentType (byte prefix) {
		for (int i = 0; i < componentTypes.length; i++) {
			if (componentTypes[i].id == prefix) {
				return componentTypes[i].name;
			}
		}
		return null; 
	}
	
	public static String getComponentType (int id) {
		byte prefix = getPrefixById(id);
		for (int i = 0; i < componentTypes.length; i++) {
			if (componentTypes[i].id == prefix) {
				return componentTypes[i].name;
			}
		}
		return null; 
	}

	public static String getComponentExtension (byte prefix) {
		for (int i = 0; i < componentExtension.length; i++) {
			if (componentExtension[i].id == prefix) {
				return componentExtension[i].ext;
			}
		}
		return null; 
	}
	
	public static String getComponentExtension (int id) {
		byte prefix = getPrefixById(id);
		for (int i = 0; i < componentExtension.length; i++) {
			if (componentExtension[i].id == prefix) {
				return componentExtension[i].ext;
			}
		}
		return null; 
	}
	
	public static class ComponentType {
		public byte id;
		public String name;
		
		public ComponentType (byte id, String name) {
			this.id = id;
			this.name = name;
		}
	}

	public static class ComponentExtension {
		public byte id;
		public String ext;
		
		public ComponentExtension (byte id, String ext) {
			this.id = id;
			this.ext = ext;
		}
	}
}
